package com.ciwong.epaper.widget.evaluate;

import com.ciwong.epaper.widget.PlayStateView;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

/**
 * Created by lzr on 2017/4/18.
 */
public class BaseRepFrameLayout extends FrameLayout
{
    private PlayStateView stateImg;

    private ImageView play_iocn;

    private long userId;

    public BaseRepFrameLayout(Context context)
    {
        super(context);
    }

    public BaseRepFrameLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public BaseRepFrameLayout(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public void initData(PlayStateView stateImg, ImageView playIocn,
            long userId)
    {
        this.stateImg = stateImg;
        this.play_iocn = playIocn;
        this.userId = userId;
    }

    public void closeStateIm(boolean mIsUnAutoMode)
    {
        if (stateImg != null)
            stateImg.closeView(mIsUnAutoMode);
    }

    /**
     * 开始录音语句
     */
    public void startRecord(long timeLong, boolean mIsUnAutoMode)
    {
        stateImg.setTimeCount(timeLong);
        if (mIsUnAutoMode)
        {
            stateImg.startUnAutoRecord();
        }
        else
        {
            stateImg.startRecord();
        }
    }

    public void updatePlayProgress(long total, long progress)
    {
        stateImg.updatePlayProgress(total, progress);
    }

    /**
     * 获取指定的SpeechTextView
     * <p>
     * 句子
     *
     * @return {@link SpeechTextView}
     */
    public PlayStateView getStateImg()
    {
        return stateImg;
    }

    /**
     * 开始播放语句
     */
    public void play(long timeLong)
    {
        stateImg.setTimeCount(timeLong);
        stateImg.startPlay();
    }

    /**
     * 设置手动播放图标的显示状态
     *
     * @param isPlay
     */
    public void playUnAto(boolean isPlay)
    {
        if (play_iocn == null)
        {
            return;
        }
        if (isPlay)
        {
            play_iocn.setVisibility(View.VISIBLE);
            Drawable drawable = play_iocn.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable)
            {
                ((AnimationDrawable) drawable).start();
            }

        }
        else
        {
            Drawable drawable = play_iocn.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable)
            {
                ((AnimationDrawable) drawable).stop();
            }
        }
    }
}
